Java Technologies উদাহরণ সহ BigInteger Arithmetic Operations গাইড ও নোট

261

BigInteger ক্লাসটি java.math প্যাকেজের একটি অংশ যা অবিচ্ছিন্ন সঠিকতার (arbitrary precision) পূর্ণসংখ্যার গণনা করতে ব্যবহৃত হয়। এটি সাধারণ int এবং long টাইপের তুলনায় অনেক বড় সংখ্যার জন্য কাজ করে এবং বিভিন্ন গাণিতিক অপারেশন যেমন যোগ, বিয়োগ, গুণ এবং ভাগ সমর্থন করে।

এখানে BigInteger ক্লাসের গাণিতিক অপারেশন (Arithmetic Operations) সম্পর্কিত উদাহরণ দেওয়া হলো।

BigInteger Arithmetic Operations উদাহরণ

1. BigInteger যোগফল (Addition)

import java.math.BigInteger;

public class BigIntegerAddition {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("123456789123456789123456789123456789");
        BigInteger number2 = new BigInteger("987654321987654321987654321987654321");

        // Adding two BigInteger numbers
        BigInteger sum = number1.add(number2);
        System.out.println("Sum: " + sum);
    }
}

আউটপুট:

Sum: 1111111111111111111111111111111111110

ব্যাখ্যা:

  • add(BigInteger val): দুটি BigInteger অবজেক্ট যোগ করার জন্য add() মেথড ব্যবহার করা হয়েছে।

2. BigInteger বিয়োগফল (Subtraction)

import java.math.BigInteger;

public class BigIntegerSubtraction {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789123456789123456789123456789");

        // Subtracting two BigInteger numbers
        BigInteger difference = number1.subtract(number2);
        System.out.println("Difference: " + difference);
    }
}

আউটপুট:

Difference: 864197532864197532864197532864197532

ব্যাখ্যা:

  • subtract(BigInteger val): দুটি BigInteger অবজেক্টের বিয়োগফল বের করার জন্য subtract() মেথড ব্যবহার করা হয়েছে।

3. BigInteger গুণফল (Multiplication)

import java.math.BigInteger;

public class BigIntegerMultiplication {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("123456789");
        BigInteger number2 = new BigInteger("987654321");

        // Multiplying two BigInteger numbers
        BigInteger product = number1.multiply(number2);
        System.out.println("Product: " + product);
    }
}

আউটপুট:

Product: 121932631112635269

ব্যাখ্যা:

  • multiply(BigInteger val): দুটি BigInteger অবজেক্ট গুণফল বের করার জন্য multiply() মেথড ব্যবহার করা হয়েছে।

4. BigInteger ভাগফল (Division)

import java.math.BigInteger;

public class BigIntegerDivision {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789");

        // Dividing two BigInteger numbers
        BigInteger quotient = number1.divide(number2);
        System.out.println("Quotient: " + quotient);
    }
}

আউটপুট:

Quotient: 8000000000

ব্যাখ্যা:

  • divide(BigInteger val): দুটি BigInteger অবজেক্টের ভাগফল বের করার জন্য divide() মেথড ব্যবহার করা হয়েছে।

5. BigInteger মোড (Modulus)

import java.math.BigInteger;

public class BigIntegerModulus {
    public static void main(String[] args) {
        // Creating two BigInteger objects
        BigInteger number1 = new BigInteger("987654321987654321987654321987654321");
        BigInteger number2 = new BigInteger("123456789");

        // Modulo operation on BigInteger
        BigInteger modulusResult = number1.mod(number2);
        System.out.println("Modulus: " + modulusResult);
    }
}

আউটপুট:

Modulus: 8000000000

ব্যাখ্যা:

  • mod(BigInteger m): এটি BigInteger অবজেক্টে আরেকটি BigInteger এর মডুলাস (বাকি) বের করার জন্য ব্যবহৃত হয়।

6. BigInteger পাওয়ার (Exponentiation)

import java.math.BigInteger;

public class BigIntegerExponentiation {
    public static void main(String[] args) {
        // Creating a BigInteger object
        BigInteger number = new BigInteger("2");

        // Raising the number to the power of 10
        BigInteger result = number.pow(10);
        System.out.println("2 to the power of 10: " + result);
    }
}

আউটপুট:

2 to the power of 10: 1024

ব্যাখ্যা:

  • pow(int exponent): এটি একটি বড় সংখ্যাকে নির্দিষ্ট পদ্ধতিতে উত্তোলন করে (যেমন কিউব বা বর্গমূল)।

7. BigInteger প্রাইম চেক (Prime Check)

import java.math.BigInteger;

public class BigIntegerPrimeCheck {
    public static void main(String[] args) {
        // Creating a BigInteger object
        BigInteger number = new BigInteger("131");

        // Check if the number is prime
        boolean isPrime = number.isProbablePrime(1); // Certainty factor = 1
        System.out.println("Is the number prime? " + isPrime);
    }
}

আউটপুট:

Is the number prime? true

ব্যাখ্যা:

  • isProbablePrime(int certainty): এটি একটি বড় সংখ্যার প্রাইম চেক করার ফাংশন। certainty হল একটি নির্ভরযোগ্যতা পরিমাণ, যেটি 0 থেকে 100 পর্যন্ত হতে পারে।

BigInteger এর অন্যান্য কার্যকারিতা:

  • gcd(BigInteger val): দুটি সংখ্যার গরিষ্ঠ সাধারণ গুণফল বের করে।
  • lcm(BigInteger val): দুটি সংখ্যার সর্বনিম্ন সাধারণ গুণফল বের করে।
  • abs(): সংখ্যার আবসোলিউট মান (অথবা ধনাত্মক মান) বের করে।

সারাংশ:

  • BigInteger হল একটি শক্তিশালী ক্লাস যা অসীম দৈর্ঘ্যের পূর্ণসংখ্যা হিসাব করতে সহায়তা করে এবং বিভিন্ন গাণিতিক অপারেশন যেমন যোগফল, বিয়োগফল, গুণফল, ভাগফল, পাওয়ার, প্রাইম চেক ইত্যাদি পরিচালনা করতে পারে।
  • এটি গাণিতিক এবং অর্থনৈতিক অ্যাপ্লিকেশনগুলিতে ব্যবহৃত হয় যেখানে বড় সংখ্যার প্রিসিশন বা সঠিকতা প্রয়োজন।
  • BigInteger ইমিউটেবল (immutable) হওয়ায় এর মান পরিবর্তন করা সম্ভব নয় এবং এটি thread-safe
Content added By
Promotion

Are you sure to start over?

Loading...